package Chapter12;

public class demo01main {
    public static void main(String[] args) {
        Cinema cinema = new Cinema();
        cinema.sellTickets();
    }
}

class Ticket {
    private int fivenum = 3;
    private int tennum = 0;
    private int twentynum = 0;

    public synchronized void sellTicket(int Money) {
        if (Money == 5) {
            fivenum++;
            System.out.println(Thread.currentThread().getName() + "给售票员5元钱，这是您的入场券");
        } else if (Money == 10) {
            while (fivenum < 1) {
                try {
                    System.out.println(Thread.currentThread().getName() + "靠边等");
                    wait();
                    System.out.println(Thread.currentThread().getName() + "结束等待");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            fivenum--;tennum++;
            System.out.println(Thread.currentThread().getName() + "给售票员10元钱,找他5元,这是您的入场券");
        } else if (Money == 20) {
            while (fivenum < 1 || tennum < 1) {
                try {
                    System.out.println(Thread.currentThread().getName() + "在一旁等待");
                    wait();
                    System.out.println(Thread.currentThread().getName() + "结束等待，开始");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            fivenum--;tennum--;twentynum++;
            System.out.println(Thread.currentThread().getName() + "给售票员20元钱，找他5元和10元，这是您的入场券");
        }
        notifyAll();
    }
}

class Cinema {
    private Thread Zhangmou, Sunmou, Zhaomou;
    private Ticket sellers;

    public Cinema() {
        sellers = new Ticket();
        Zhangmou = new Thread(new Runnable() {
            @Override
            public void run() {
                sellers.sellTicket(20);
            }}, "张某");
        Sunmou = new Thread(new Runnable() {
            @Override
            public void run() {
                sellers.sellTicket(10);
            }}, "李某");
        Zhaomou = new Thread(new Runnable() {
            @Override
            public void run() {
                sellers.sellTicket(5);
            }}, "赵某");
    }

    public void sellTickets() {
        Zhangmou.start();
        Sunmou.start();
        Zhaomou.start();
    }
}